﻿#include "stdafx.h"
#include "ServerSocket.h"
#include <sstream>
#include <iomanip>
using namespace std;
CServerSocket::CServerSocket()
{
	m_userName="";
	name="";
	host="";
	cur_db = "";
}

CServerSocket::~CServerSocket()
{
}

//添加上线用户
void CServerSocket::OnAccept(int nErrorCode)
{
	m_pDlg->AddClient();
	CSocket::OnAccept(nErrorCode);
}

// 删除下线用户
void CServerSocket::OnClose(int nErrorCode)
{
	m_pDlg->UpdateEvent(this->m_userName+L" ", L"断开连接 ！");
	m_pDlg->RemoveClient(this);
	CSocket::OnClose(nErrorCode);
}

// 接收数据
void CServerSocket::OnReceive(int nErrorCode)
{
	this->startTime= clock();
	m_pDlg->RecvData(this);
	CSocket::OnReceive(nErrorCode);
}
double round(double number, unsigned int bits) {
	stringstream ss;
	ss << fixed << setprecision(bits) << number;
	ss >> number;
	return number;
}

void CServerSocket::SendMSG(string str) {
	//发送大量信息
	double interval = difftime(clock() , this->startTime);
	interval = interval /1000;
	str = str +"\r\n"+to_string( interval)+" sec";
	while (str.length() > 1024) {
		string temp = "1";
		temp += str.substr(0, 1023);
		str = str.substr(1023, str.length() - 1023);
		this->Send(temp.c_str(), temp.length());
	}
	str = "0" + str;
	this->Send(str.c_str(), str.length());
}
